package com.lx.pc.restful;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import com.lx.pc.domain.vo.user.UserCookieQo;
import com.lx.pc.domain.vo.user.UserVo;
import com.lx.pc.restful.usercookie.UserCookie;
import com.lx.pc.service.user.IUserService;

public class BaseController {
	
//	@Resource(name = "springRedisTemplate")
//	private SpringRedisTemplate springRedisTemplate;

	@Resource(name = "userService")
	private IUserService userService;

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
	}

	/**
	 * 构建ModelAndView
	 * 
	 * @param 响应页面
	 * @return ModelAndView
	 */
	@SuppressWarnings("unused")
	protected ModelAndView getModelAndView(String view, HttpServletRequest request){
		ModelAndView mav = null;
		if (StringUtils.isNotBlank(view)) {
			mav = new ModelAndView(view);
		} else {
			mav = new ModelAndView();
		}
		UserCookie userCookie = new UserCookie();
		UserCookieQo userCookieQo = userCookie.getUserInfoCookie(request);
		if (userCookieQo != null) {
			String userId = userCookieQo.getId();
			UserVo userInfo = null;
			if (StringUtils.isNotBlank(userId)) {
				String sessionKey = "SESSION_" + userId;
//				if (springRedisTemplate.exists(sessionKey)) {
//					userId = (String)springRedisTemplate.read(sessionKey);
//					userInfo = userBusiness.getUserInfo(userId);
//				}
				BigInteger uId = new BigInteger(userId);
				userInfo = userService.find(uId);
			}
			if (userInfo != null) {
				mav.addObject("userInfo", userInfo);
			}
			
		}
		// 缓存用户登录转发页面
//		String path = request.getContextPath();
//		String servletPath = request.getServletPath();  
//		String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + servletPath;
//		String query = request.getQueryString();
//		if (!StringUtils.isEmpty(query)) {
//			url += "?" + query;
//		}
//		mav.addObject("redirectUrl", url);
//		
//		mav.addObject("path", path);
		
		//mav.addObject("exposeTypes", TalkExposeEnums.ExposeType.values());
		
		//mav.addObject("fileServer", CommonSettingsUtil.getInstance().getHttpServer());
		return mav;
	}

	/**
	 * 保存用户登录信息
	 * 
	 */
	protected void setUserInfo(UserVo userInfo, HttpServletRequest request) {
		if (userInfo != null) {
			//String userId = userInfo.getId().toString();
			//String sessionKey = "SESSION_" + userId;
//			if (springRedisTemplate.exists(sessionKey)) {
//				springRedisTemplate.delete(sessionKey);
//			}
//			springRedisTemplate.save(sessionKey, userInfo.getId());
		}
	}

	/**
	 * 获取用户登录信息
	 * 
	 */
	protected UserVo getUserInfo(HttpServletRequest request) {
		UserCookie userCookie = new UserCookie();
		UserCookieQo userCookieQo = userCookie.getUserInfoCookie(request);
		if (userCookieQo == null) {
			return null;
		}
		String userId = userCookieQo.getId();
		if (StringUtils.isEmpty(userId)) {
			return null;
		}
		BigInteger id = new BigInteger(userId);
		UserVo userInfo = userService.find(id);
		return userInfo;
	}
	
}
